from file_define import TextFileReader,JsonFileReader,FileReader
from data_define import Record
from pymysql import Connection

con = Connection(
    host = "localhost",
    port = 3306,
    user= "root",
    password="123456"
)

cursor = con.cursor()

con.select_db("test")

cursor.execute("select * from orders")
result = cursor.fetchall()

f = open("D:/MyData.txt","w",encoding="UTF-8")
f.close()
f = open("D:/MyData.txt","a",encoding="UTF-8")

record_list:list[Record] = []

for record in result:
    r = Record(record[0],record[1],record[2],record[3])
    record_list.append(r)

for record in record_list:
    f.write("{")
    f.write(f"\"date\":\"{record.date}\","
            f"\"order_id\":\"{record.order_id}\","
            f"\"money\":\"{record.money}\","
            f"\"province\":\"{record.province}\"")
    f.write("}\n")

# for record in result:
#     f.write("{")
#     f.write(f"\"date\":\"{record[0]}\","
#             f"\"order_id\":\"{record[1]}\","
#             f"\"money\":\"{record[2]}\","
#             f"\"province\":\"{record[3]}\"")
#     f.write("}\n")

f.close()
con.close()